//
//  CinemaViewController.m
//  豆瓣HW
//
//  Created by dllo on 15/11/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaTableViewCell.h"
#import "Cinema.h"

@interface CinemaViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *cinemaArr;

@end

@implementation CinemaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"影院";
    
    [self getData];
    
    
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [_tableV release];
    
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    while (self.cinemaArr.count == 0) {
        [self.tableV reloadData];
    }
}

- (void)getData
{

    // 4, 创建url
    NSURL *url = [NSURL URLWithString:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/cinemalist.php"];
    
    // 3, 创建请求对象, request可设置信息如url,方式(GET/POST),超时时间等
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    
    // 1, 创建NSURLSession对象, 选择默认配置
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    // 2, 创建请求任务, 当异步请求完成会调用block,在block内完成数据处理
    NSURLSessionDataTask *getTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.cinemaArr = [NSMutableArray array];
        NSMutableDictionary *dic1 = [dic objectForKey:@"result"];
        NSMutableArray *arr = [dic1 objectForKey:@"data"];
        
        for (NSMutableDictionary *tempDic in arr) {
            Cinema *cin = [[Cinema alloc] init];
            [cin setValuesForKeysWithDictionary:tempDic];
            [self.cinemaArr addObject:cin];
            [cin release];
//            NSLog(@"%@", cin);
            
            [self.tableV reloadData];
        }
        
    }];

    // 开始任务
    [getTask resume];
    




}

// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    Cinema *cin = [self.cinemaArr objectAtIndex:indexPath.row];
//    
//    return [CinemaTableViewCell heightWithStr:cin.address];
    return self.view.frame.size.height / 4;
    
}
// 每个区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

// 铺建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    CinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[[CinemaTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr] autorelease];
    }
    Cinema *cin = [self.cinemaArr objectAtIndex:indexPath.row];
    cell.labelName.text = cin.cinemaName;
    cell.labelAddress.text = cin.address;
    cell.labelTelephone.text = cin.telephone;
    
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
